﻿package {
	import flash.display.*;
	import flash.events.*;
	import flash.net.URLRequest;
	import flash.geom.*;
	import flash.filters.*;

	/**
	 * @author CYPL
	 * 设置图片元件实例名为Image
	 */
	public class CutImageTest extends Sprite {
		private var _imageBitmapData : BitmapData;
		private var _imageHotAreaData:BitmapData;
		private var _imageBitmap : Bitmap;
		private var _mouseRectContainer:Sprite;
		private var _mouseRectStartX:Number;
		private var _mouseRectStartY:Number;
		private var _imageClipDraging:Boolean;
		private var _currentDragClip:Sprite;
		public function CutImageTest() {
			
			_mouseRectContainer=new Sprite;
			Image.visible=false;
			_imageBitmapData=new BitmapData(Image.width,Image.height,true,0);
			_imageBitmapData.draw(Image);
			_imageBitmap=Bitmap(addChild(new Bitmap(_imageBitmapData)))
			_imageBitmap.x=30
			_imageBitmap.y=30
			configMouseEvent();
			//----------hitTestArea------------------------
			var c:ColorTransform=new ColorTransform;
			c.color=0xff0000;
			_imageHotAreaData=_imageBitmapData.clone();
			_imageHotAreaData.draw(_imageHotAreaData,null,c);
		}

		private function configMouseEvent():void {
			stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);
			stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);
		}
		/**************************drawRect handler*******************************/
		private function mouseDownHandler(evt:MouseEvent):void {//mouse_down

			if (_imageClipDraging) {
				return;
			}
			addChild(_mouseRectContainer);
			_mouseRectStartX=evt.stageX;
			_mouseRectStartY=evt.stageY;
			stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);
		}
		private function mouseUpHandler(evt:MouseEvent):void {//mouse_up
			
			//_currentDragClip&&();
			_imageClipDraging&&(_currentDragClip.stopDrag(),_imageClipDraging=false,_currentDragClip.alpha=1)||(cutImage(checkIntersection()),_mouseRectContainer.graphics.clear(),stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler))
			
		}
		private function mouseMoveHandler(evt:MouseEvent):void {//mouse_move
			evt.updateAfterEvent();
			var minX:Number=Math.min(evt.stageX,_mouseRectStartX)
			var minY:Number=Math.min(evt.stageY,_mouseRectStartY)
			var maxX:Number=Math.max(evt.stageX,_mouseRectStartX)
			var maxY:Number=Math.max(evt.stageY,_mouseRectStartY)

			with(_mouseRectContainer.graphics){
				clear();
				lineStyle(0);
				beginFill(0xffff00,.5);
				drawRect(0,0,maxX-minX,maxY-minY);
			}
			_mouseRectContainer.x=minX;
			_mouseRectContainer.y=minY;
		}
		/************************************************************************/
		/**************************drag  handler*******************************/
		private function clipMouseDownHandler(evt:MouseEvent):void {//mouse_down
			var target:Sprite=evt.target as Sprite;
			_currentDragClip=target;
			_currentDragClip.alpha=.5;
			_imageClipDraging=true;
			addChild(target);
			_currentDragClip.startDrag(false);

		}
		/************************************************************************/

		private function checkIntersection():Rectangle {
			var intersectRect:Rectangle=_imageBitmapData.rect.intersection(new Rectangle(_mouseRectContainer.x-_imageBitmap.x,_mouseRectContainer.y-_imageBitmap.y,_mouseRectContainer.width,_mouseRectContainer.height));
			trace("与源图BitmapData相交范围:"+intersectRect);
			if (intersectRect.width==0 || intersectRect.height==0) {
				return null;
			}
			var bitmapData:BitmapData=new BitmapData(intersectRect.width,intersectRect.height,true,0);
			bitmapData.draw(_imageHotAreaData,new Matrix(1,0,0,1,-intersectRect.x,-intersectRect.y),null,null,new Rectangle(0,0,intersectRect.width,intersectRect.height));
			var intersectHotAreaRect:Rectangle=bitmapData.getColorBoundsRect(0xFFFF0000, 0xFFFF0000,true);
			trace("最终切图块的范围:"+intersectHotAreaRect);
			if (intersectHotAreaRect.width==0 || intersectHotAreaRect.height==0) {
				
				
				return null;
			}
			//扩展范围避免误差
			intersectHotAreaRect.x-=1
			intersectHotAreaRect.y-=1
			intersectHotAreaRect.width+=2
			intersectHotAreaRect.height+=2
			return intersectHotAreaRect;
		}
		private function cutImage(rect:Rectangle):void {//关键的切图部分
			if (!rect) {
				return;
			}
			var clipBitmapData:BitmapData=new BitmapData(rect.width,rect.height,true,0);
			var cliptX:Number=(_mouseRectContainer.x=_mouseRectContainer.x<_imageBitmap.x?0:_mouseRectContainer.x-_imageBitmap.x)+rect.x;
			var cliptY:Number=(_mouseRectContainer.y=_mouseRectContainer.y<_imageBitmap.y?0:_mouseRectContainer.y-_imageBitmap.y)+rect.y;
			clipBitmapData.draw(_imageBitmapData,new Matrix(1,0,0,1,-cliptX,-cliptY),null,null,new Rectangle(0,0,rect.width,rect.height));//intersectHotAreaRect)
			var clipBitmap:Bitmap=new Bitmap(clipBitmapData);
			clipBitmap.filters=[new GlowFilter(0,1,2,2,10,1)];
			var sprite:Sprite=new Sprite
			with(sprite.graphics){
				lineStyle(0);
				lineTo(rect.width,0);
				lineTo(rect.width,rect.height);
				lineTo(0,rect.height);
				lineTo(0,0);
			}
			sprite.x=_mouseRectContainer.x+rect.x+_imageBitmap.x
			sprite.y=_mouseRectContainer.y+rect.y+_imageBitmap.y
			sprite.addEventListener(MouseEvent.MOUSE_DOWN,clipMouseDownHandler);
			Sprite(addChild(sprite)).addChild(clipBitmap)
			var fillRect:Rectangle=new Rectangle(sprite.x-_imageBitmap.x,sprite.y-_imageBitmap.y,rect.width,rect.height);
			_imageBitmapData.fillRect(fillRect,0);
			_imageHotAreaData.fillRect(fillRect,0);
			
		}
	}
}